import Mock from "mockjs";
// 假数据添加
var result = Mock.mock({
    "data|31": [
        {
        //id
        "cartid|+1": 1,
        //商品图片
        goodsImg: "@Image('100x100','@color','商品')",
        //产品名称
        "cartname|1":["丝袜奶茶","鸳鸯奶茶","伯爵奶茶","冰布丁奶茶","冰绿抹奶茶","仙草奶茶","珍珠奶茶","热可可奶茶","玫瑰奶茶","皇家奶茶","丝滑奶茶","港式奶茶","玫瑰香芋奶茶","玫瑰奶茶西米露","蜜爽绿豆奶茶","红豆奶茶"],
        //编号
        "id":/^(NC)\d{6}$/,
        //添加时间
        "time":'@date("yyyy-MM-dd")',
        //浏览量
        "browsenum|+1": 1500,
        //搜索量
        "searchnum|+3": 1600,
        //状态
        "flag|1":[true,false],
        },
      ],
});

class StudentsService {
    //获取商品
    getDate() {
      return Promise.resolve({
        result,
      });
    }
    //修改状态
    upDate(status,id){
      result.data.map((itme)=>{
        if(itme.cartid===id){
          itme.flag = status
        }
        return itme
      })
        return Promise.resolve({result});
    }
    //删除商品
    delDate(name){
        result.data = result.data.filter((item) => item.cartid !== name);
        return Promise.resolve({result});
    }
    //修改单个商品
    upDatecart(form){
      result.data = result.data.filter((item) => {
        if(item.id == form.id){
            if(form.goodsImg){
              item.goodsImg = form.goodsImg
            }
            item.cartname = form.cartname;
            item.time = form.time===null?item.time:form.time;
            item.browsenum=form.browsenum
            item.searchnum=form.searchnum
            item.flag = form.flag
        }
        return item
      });
      return Promise.resolve({result});
    }
    //添加商品
    addCart(form){
      console.log(form);
      form.cartid=result.data.length+1
      form.flag==="显示"?true:false;
      form.browsenum=0;
      form.searchnum=0;
      result.data.push(form);
      return Promise.resolve({result});
    }
}
const service = new StudentsService();
export default service;
